﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OauthProvider;

namespace Chat_WebServer
{
    [Serializable]
    public class MiniAccessToken : TokenBase
    {
        public string UserName { get; set; }
        public string[] Roles { get; set; }
        public DateTime ExpireyDate { get; set; }

        public override void SaveToken()
        {
            using (var db = new AccountEntities())
            {
                AccessToken tk = new AccessToken();
                tk.ConsumerKey = ConsumerKey;
                tk.Realm = Realm;
                tk.TokenSecret = TokenSecret;
                tk.ExpireyDate = ExpireyDate;
                tk.UserName = UserName;
                tk.Token = Token;
                var accesstoken = db.AccessTokens.ToList().FirstOrDefault(r => r.Token == Token);
                if (accesstoken != null)
                    accesstoken = tk;
                else db.AccessTokens.AddObject(tk);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new Exception("Can not save AccessToken because: " + ex.ToString());
                }
            }
        }
    }
}
